package com.kakarote.admin.common;

import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;

public enum AdminIntegralTypeEnum {

    /**
     * 积分记录类型（1 邀请注册，2 受邀请注册，3 兑换礼品，4.订单佣金）
     */
    invitation(1L, 100L, 1L),
    sufferInvitation(2L, 100L, 1L),
    exchangeGifts(3L, null, 0L),
    commission(4L, null, 1L);;


    /**
     * 积分记录类型（1 邀请注册，2 受邀请注册，3 兑换礼品，4.订单佣金）
     */
    @Getter
    private Long type;

    /**
     * 积分
     */
    @Getter
    private Long interal;

    /**
     * 积分记录状态（0 失去，1 获取）
     */
    @Getter
    private Long status;

    AdminIntegralTypeEnum(Long type, Long interal, Long status) {
        this.type = type;
        this.interal = interal;
        this.status = status;
    }

    public static AdminIntegralTypeEnum parse(Long type) {
        return Arrays.stream(AdminIntegralTypeEnum.values()).filter(e -> Objects.equals(type, e.getType())).findFirst().orElse(null);
    }

    public static AdminIntegralTypeEnum parse(Long type, Long interal) {
        AdminIntegralTypeEnum adminIntegralTypeEnum = Arrays.stream(AdminIntegralTypeEnum.values()).filter(e -> type == e.getType()).findFirst().orElse(null);
        if (adminIntegralTypeEnum.getType() == 3) { // 兑换礼品类型时，积分自己设置
            adminIntegralTypeEnum.interal = interal;
        }
        return adminIntegralTypeEnum;
    }

    /**
     * 计算积分
     *
     * @param num
     * @return
     */
    public Long calculateInteral(Long num) {
        if (this.status == 0) {
            return num = num - this.interal;
        } else if (this.status == 1) {
            return num = num + this.interal;
        }
        return this.interal;
    }
}
